package com.dd.stack.mybatisplus.service.task.impl;

import com.dd.stack.mybatisplus.mapper.varify_explain_type.ExplainTypeVerificationOrderMapper;
import com.dd.stack.mybatisplus.mapper.varify_explain_type.ExplainTypeVerificationUserMapper;
import com.dd.stack.mybatisplus.pojo.DO.varify_explain_type.ExplainTypeVerificationOrderDO;
import com.dd.stack.mybatisplus.pojo.DO.varify_explain_type.ExplainTypeVerificationUserDO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/3/25 23:24
 * @Description 验证MySQL内置性能分析工具EXPLAIN得出的type列值 -> 性能 system > const > eq_ref > ref > range > index > ALL
 * @Target 🎯生成测试数据
 */
@Service
@RequiredArgsConstructor
public class ExplainTypeVerificationTask {

    private static final Random random = new Random();
    private final ExplainTypeVerificationUserMapper userMapper;
    private final ExplainTypeVerificationOrderMapper orderMapper;

    // 生成用户数据
    public void geneTestData() {
        // 生成用户数据
        List<ExplainTypeVerificationUserDO> users = IntStream.rangeClosed(1, 10000)
                .mapToObj(i -> new ExplainTypeVerificationUserDO(
                        "user" + i,
                        "user" + i + "@example.com",
                        random.nextInt(80) + 18
                )).collect(Collectors.toList());

        // 生成订单数据
        List<ExplainTypeVerificationOrderDO> orders = IntStream.rangeClosed(1, 10000)
                .mapToObj(i -> new ExplainTypeVerificationOrderDO(
                        (long) random.nextInt(10000) + 1,
                        BigDecimal.valueOf(random.nextDouble() * 1000),
                        random.nextBoolean() ? "paid" : "shipped"
                )).collect(Collectors.toList());

        // 批量插入（建议分批次，如每次1000条）
        users.forEach(userMapper::insert);
        orders.forEach(orderMapper::insert);
    }
}
